import React, { useState, useEffect } from 'react'
import { stuList, deleteStu, addStu, search, updateStu ,paginationList} from '../../services/students'

import List from './List'
export default function () {

  const [stuArr, setStuArr] = useState([]);
  const [pagination,setPagination] = useState({
    showSizeChanger:true,
    showQuickJumper:true,
    pageSizeOptions:[5,10,15,20,25],
    showTotal:total => `共 ${total} 条`
  })

  const getStuInfo = async () => {
    let result = await stuList()
    setStuArr(result)
  }

  //获取分页数据信息
  const getPagination = async (current,pageSize) => {
    let result = await paginationList({current,pageSize})
    result.current = parseInt(result.current)
    setPagination({...pagination,...result})
  }

  useEffect(() => {
    getStuInfo()
    getPagination(1,5)
  }, [0])

  const toAdd = async (stuObj) => {
    await addStu(stuObj)
    getStuInfo()
  }

  const toDelete = async (_id) => {
    await deleteStu(_id)
    getStuInfo()
  }

  const toSearch = async (type, searchContent) => {
    if(type === 'all'||type===''){
      getStuInfo()
    }else{
      let result = await search({ type, searchContent })
      setStuArr(result)
    }
  }

  const toUpdate = async (data) => {
    await updateStu(data)
    getStuInfo()
  }

  return (
    <div >
        <List stuArr={stuArr} toAdd={toAdd} toDelete={toDelete} toUpdate={toUpdate} toSearch={toSearch}
        pagination={pagination} getPagination={getPagination}/>
    </div>
  )

}
